<?php

namespace App\Admin\Controllers;

use App\Models\Behavior;

use App\Models\Goods;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class BehaviorController extends Controller
{
    use ModelForm;

    public $header = '行为列表';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Behavior::class, function (Grid $grid) {

            $grid->id('编号')->sortable();
            $grid->name('名称');
            $grid->score('分值')->display(function ($v){
                return $this->type == 1 ? '+' . $v : '-' . $v;
            });
            $grid->type('类型')->using([
                '1' => '积极的',
                '2' => '消极的'
            ]);
            $grid->img()->url('图标')->image('','50');

            $grid->cat()->name('行为分类');

            $grid->created_at('添加时间');


            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableView();
            });

            $grid->filter(function (Grid\Filter $filter){
                $filter->disableIdFilter();
                $filter->like('name', '名称');
                $filter->equal('type', '类型')->select([
                    '1'=>'积极的',
                    '2'=>'消极的'
                ]);
                $filter->equal('cat_id', '行为分类')->select(
                    Behavior\Cat::all()->pluck('name','id')
                );
                $filter->between('created_at', '添加时间')->datetime();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Behavior::class, function (Form $form) {

            $form->display('id', '编号');
            $form->text('name', '名称');
            $states = [
                'on'  => ['value' => 1, 'text' => '积极的', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '消极的', 'color' => 'danger'],
            ];
            $form->switch('type', '类型')->states($states)->default('1');
            /*$form->radio('type', '类型')->options([
                '1' => '积极的',
                '2' => '消极的'
            ]);*/
            $form->number('score', '分值')->rules('required|min:1')->min(1);

            $cat = Behavior\Cat::all()->pluck('name', 'id')->toArray();
            $form->radio('cat_id', '行为标签')->options($cat);

            $a = Behavior\Image::options();

            $form->radio('image_id', '行为头像')->options($a);


            /*$icon = '';
            $form->radio('image_id', '行为头像')->options(function ($v) use (&$icon){
                $icon = $v;
                return Behavior\Image::options();
            })->checked([$icon]);*/

            $form->textarea('note', '备注');
            $form->display('created_at', '创建时间');

            $form->disableViewCheck();
        });
    }



}
